<template>
  
</template>
<script>
import Api from "../../kites/api";
export default {
  name: "Auth",
  data() {
    return {};
  },
  mounted() {
      this.auth();
  },
  methods: {
    auth() {
      var me = this;
      var _config;
      $.ajax({
        url: Api.juris,
        type: "get",
        data: "",
        dataType: "json",
        async: false,
        contentType: "application/json",
        success: function(res) {
            _config = {
                corpId: res.data.corpId,
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                signature: res.data.signature,
                agentId: res.data.agentId,
                rebackUrl: res.data.url,
                jsticket: res.data.jsticket
            };

            dd.config({
                agentId: _config.agentId,
                corpId: _config.corpId,
                timeStamp: _config.timeStamp,
                nonceStr: _config.nonceStr,
                signature: _config.signature,
                jsApiList: [
                "runtime.info",
                "biz.contact.choose",
                "device.notification.confirm",
                "device.notification.alert",
                "device.notification.prompt",
                "biz.ding.post",
                "biz.user.get",
                "biz.util.openLink"
                ]
            });

            dd.ready(function() {
                dd.biz.user.get({
                onSuccess: function(info) {
                    $.ajax({
                    url:Api.openid + info.id,
                    type: "get",
                    data: "",
                    dataType: "json",
                    async: false,
                    contentType: "application/json",
                    success: function(res) {
                        if (res.isOK) {
                            localStorage.Login = res.data.Login;
                            localStorage.Token = res.data.Token;
                            me.$axios.defaults.headers.common['token']=localStorage.Token
                            me.$router.push("/home");
                        } else {
                            localStorage.openid = info.id;
                            me.$router.push("/Login");
                        }
                    },
                    error: function(res) {
                            localStorage.openid = info.id;
                            me.$router.push("/Login");
                    }
                    });
                },
                onFail: function(err) {
                    alert("dd error: " + JSON.stringify(err));
                    me.$router.push("/Login");
                    logger.e("userGet fail: " + JSON.stringify(err));
                }
                });
            });

            dd.error(function(err) {
                alert("dd error: " + JSON.stringify(err));
                me.$router.push("/Login");
            });
            me.$router.push("/Login");
        },
        error: function(error) {
          me.$router.push("/Login");
        }
      });
      setTimeout(()=>{
          me.$router.push("/Login");
      },0)
    }
  }
};
</script>
<style scoped>

</style>


